package com.cty.A_GeneralDataStructures.B_LinkedList;


import com.cty.F_common.DataItem;

/**
 * @Auther: cty
 * @Date: 2020/6/13 21:41
 * @Description: 测试单链表和迭代器
 * @version: 1.0
 */
public class LinkedListApp {
    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        ListIterator iterator = list.getIterator();
        DataItem findItem;
        DataItem delItem;

        System.out.println("测试 insertFirst()");
        list.insertFirst(new DataItem(11));
        list.insertFirst(new DataItem(22));
        list.insertFirst(new DataItem(33));
        list.insertFirst(new DataItem(44));
        list.displayList();
        System.out.println("---------------------------------------------------------");

        System.out.println("测试 insertAfter()");
        list.insertAfter(11, new DataItem(55));
        list.insertAfter(22, new DataItem(66));
        list.displayList();
        System.out.println("---------------------------------------------------------");

        System.out.println("测试 insertBefore()");
        list.insertBefore(33, new DataItem(77));
        list.insertBefore(44, new DataItem(88));
        list.displayList();
        System.out.println("---------------------------------------------------------");

        System.out.println("测试 deleteFirst()");
        delItem = list.deleteFirst();
        if(delItem != null)
            System.out.println("Deleted " + delItem.getKey());
        else
            System.out.println("Delete failed");
        list.displayList();
        System.out.println("---------------------------------------------------------");

        System.out.println("测试 delete()");
        list.delete(22);
        list.delete(33);
        list.delete(44);
        list.displayList();
        System.out.println("---------------------------------------------------------");

        System.out.println("测试 find()");
        findItem = list.find(66);
        if(findItem != null)
            System.out.println("Found " + findItem.getKey());
        else
            System.out.println("Can't find");
        System.out.println("---------------------------------------------------------");


        System.out.println("测试 ListIterator find()");
        findItem = iterator.find(55);
        if(findItem != null)
            System.out.println("Found " + findItem.getKey());
        else
            System.out.println("Can't find");
        System.out.println("---------------------------------------------------------");

        System.out.println("测试 ListIterator insertBefore()");
        iterator.insertBefore(new DataItem(77));
        list.displayList();
        System.out.println("---------------------------------------------------------");

        System.out.println("测试 ListIterator insertAfter()");
        iterator.insertAfter(new DataItem(88));
        list.displayList();
        System.out.println("---------------------------------------------------------");

        System.out.println("测试 ListIterator delete()");
        delItem = iterator.delete();
        if(delItem != null)
            System.out.println("Deleted " + delItem.getKey());
        else
            System.out.println("Delete failed");
        list.displayList();
        System.out.println("---------------------------------------------------------");

    }  // main()
}  // end LinkedListApp{}


/**
 * 测试 insertFirst()
 * 44 33 22 11
 * ---------------------------------------------------------
 * 测试 deleteFirst()
 * Deleted 44
 * 33 22 11
 * ---------------------------------------------------------
 * 测试 insertAfter()
 * 33 22 55 11 66
 * ---------------------------------------------------------
 * 测试 find()
 * Found 66
 * ---------------------------------------------------------
 * 测试 ListIterator find()
 * Found 55
 * ---------------------------------------------------------
 * 测试 ListIterator insertBefore()
 * 33 22 77 55 11 66
 * ---------------------------------------------------------
 * 测试 ListIterator insertAfter()
 * 33 22 77 88 55 11 66
 * ---------------------------------------------------------
 * 测试 ListIterator delete()
 * Deleted 88
 * 33 22 77 55 11 66
 * ---------------------------------------------------------
 */
